package top.whysu.gps.dto.page;

import cn.hutool.core.bean.BeanUtil;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页查询结果DTO
 */
public class PagenationDataDTO<T> implements Serializable {
    private static final long serialVersionUID = -4314014735623295376L;
    /**
     * 总记录数
     */
    private long totalSize;
    /**
     * 总页数
     */
    private long totalPage;
    /**
     * 当前页码
     */
    private int pageNum;
    /**
     * 每页记录数
     */
    private int pageSize;
    /**
     * 数据list
     */
    private List<T> list;

    public PagenationDataDTO(long totalSize, int totalPage, int pageNum, int pageSize, List<T> list) {
        this.totalSize = totalSize;
        this.totalPage = totalPage;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.list = list;
    }

    public PagenationDataDTO(long totalSize, int pageNum, int pageSize, List<T> list) {
        this.totalSize = totalSize;
        this.totalPage = totalSize % pageSize == 0 ? totalSize / pageSize : (totalSize / pageSize + 1);
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.list = list;
    }

    /**
     * 参数转换
     *
     * @param pagerDTO
     * @return
     */
    public static <T> Map<String, Object> beanToSqlMap(PagerDTO<T> pagerDTO) {
        // 转换查询参数
        Map<String, Object> map = new HashMap<>();
        if (pagerDTO.getParams() != null) {
            map = BeanUtil.beanToMap(pagerDTO.getParams());
        }
        // 设置分页的limit 参数
        map.put("pageSize", pagerDTO.getPager().getPageSize());
        map.put("pageNum", (pagerDTO.getPager().getPageNum() - 1) * pagerDTO.getPager().getPageSize());
        return map;
    }

    public long getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(long totalSize) {
        this.totalSize = totalSize;
    }

    public long getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(long totalPage) {
        this.totalPage = totalPage;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}
